package com.letou.ls.ui;

import java.util.ArrayList;

import com.letou.ls.R;
import com.letou.ls.mode.LotteryTypeInfo;
import com.letou.ls.uitl.Constant;
import com.letou.ls.uitl.MyUtil;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.Spinner;
import android.widget.TextView;

public class SelectActivity extends Activity implements OnClickListener {

	private TextView mTextView;
	private Button mButton;
	private Spinner mSpinner;
	private ArrayList<String> typeCodeList;
	private ArrayList<String> typeNameList;
	private ArrayList<String> expirationTimeList;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_select);
		mTextView = (TextView) findViewById(R.id.select_text);
		mSpinner = (Spinner) findViewById(R.id.select_spinner);
		mButton = (Button) findViewById(R.id.select_button);
		mButton.setOnClickListener(this);
		ArrayList<LotteryTypeInfo> lotteryTypeInfo = Constant.lotteryTypeInfo;
		typeCodeList = new ArrayList<String>();
		typeNameList = new ArrayList<String>();
		expirationTimeList = new ArrayList<String>();
		if (lotteryTypeInfo != null) {

			for (LotteryTypeInfo item : lotteryTypeInfo) {
				typeCodeList.add(item.getTypeCode());
				typeNameList.add(item.getTypeName());
				expirationTimeList.add(item.getExpirationTime());

			}

		}
		if (!typeNameList.isEmpty()) {
			mTextView.setText("您选中的彩种是:  " + typeNameList.get(0));
		}

		ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item,
				typeNameList);
		adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		mSpinner.setAdapter(adapter);

		mSpinner.setOnItemSelectedListener(new OnItemSelectedListener() {

			@Override
			public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
				// TODO Auto-generated method stub
				if (!typeNameList.isEmpty() && typeNameList.size() > position) {
					mTextView.setText("您选中的彩种是:  " + typeNameList.get(position));
				}
			}

			@Override
			public void onNothingSelected(AdapterView<?> parent) {
				// TODO Auto-generated method stub

			}

		});

	}

	@Override
	public void onClick(View arg0) {
		// TODO Auto-generated method stub
		int number = mSpinner.getSelectedItemPosition();
		String code = typeCodeList.get(number);

		Intent intent = MyUtil.getIntentById(this, code);
		if (intent == null) {
			return;
		}
		Constant.currentLotteryNumber = number;

		startActivity(intent);
		finish();
	}

}
